Bound IntegralRC correction rate to a settings-free physiological ceiling#37
Open
ps2 wants to merge 1 commit into
Open
Bound IntegralRC correction rate to a settings-free physiological ceiling#37ps2 wants to merge 1 commit into
ps2 wants to merge 1 commit into
Conversation
…ling The port from LoopKit to LoopAlgorithm dropped IntegralRetrospectiveCorrection's integral clamp — a safety bound on the wound-up integral term. Rather than restore the original clamp (which scaled the bound by ISF x basal and the target range), this bounds the RC correction RATE to a physiological ceiling (mg/dL/min), independent of user settings. Rationale: RC forecasts unmodeled physiology, whose plausible velocity does not depend on a person's insulin needs or target range. The original clamp's implied "plausible unmodeled velocity" varied ~11x across settings (0.55-4.9 mg/dL/min for ISF x basal of 18-160) - trusting the same real carb rise for a high-need user while clamping it for a low-need one. The integral is already self-limiting (leaky integrator; converges to ~1.087 x discrepancy), so the clamp is a rare-event backstop for large or spurious discrepancies, and a fixed physiological rate ceiling is the right shape for that. Default 4 mg/dL/min (top of plausible sustained unmodeled velocity). On real IRC data (two datasets) this reproduces the deployed clamp's behavior - both near-inert in normal operation - without the settings coupling. Configurable via IntegralRetrospectiveCorrection(effectDuration:maxCorrectionVelocity:); nil disables. Tests: correction rate clamped to +/- the ceiling on a large windup, symmetric for negative discrepancies, and inert when the rate is below the ceiling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The port of Loop's algorithm from LoopKit to the standalone LoopAlgorithm package dropped
IntegralRetrospectiveCorrection's integral clamp — a safety bound on the wound-up integral term. This adds a bound back, but not the original one: instead of scaling the limit byISF × basal(dosing authority) and the target range, it bounds the RC correction rate to a physiological ceiling (mg/dL/min), independent of user settings.Why not restore the original (settings-dependent) clamp
RC forecasts unmodeled physiology (the residual between observed and model-predicted glucose). The plausible velocity of that physiology — carbs, EGP, exercise, insulin-model error — does not depend on a person's insulin needs or target range.
The original LoopKit clamp bounded the integral by
zeroTempEffect = |ISF × basal|andglucose − rangeMax/rangeMin. Because the integral is already self-limiting (it's a leaky integrator:integralForget ≈ 0.92, so it converges to ≈1.087 × discrepancy), that clamp is really just deciding how large an unmodeled discrepancy to trust — and it answered that in units of dosing authority. The implied "plausible unmodeled velocity" therefore varied ~11× across settings (≈ 0.55–4.9 mg/dL/min forISF × basalof 18–160): the same real carb rise would be trusted for a high-need user and clamped as implausible for a low-need one. That's the wrong axis for a physiological plausibility bound.What this does
Bounds the correction rate to
maxCorrectionVelocity(default 4 mg/dL/min, the top of plausible sustained unmodeled glucose velocity). Since the integral self-limits, this is a rare-event backstop for large or spurious discrepancies (e.g. sensor artifacts), and a fixed physiological rate ceiling is the right shape for that.IntegralRetrospectiveCorrection(effectDuration:); configurable through the newmaxCorrectionVelocity:initializer parameter;nildisables the clamp.RetrospectiveCorrectionprotocol,generatePrediction, or any call site — the ceiling is internal to IRC.Tests
New cases in
IntegralRetrospectiveCorrectionTests(via the addedcorrectionVelocitydiagnostic): the correction rate is clamped to ±ceiling on a large windup, symmetric for negative discrepancies, and left untouched when the rate is below the ceiling. All 141 package tests pass.